import { defineStore } from 'pinia'
import { store } from '../index'

import playBackImg from '@/assets/ivr/playback.svg'
import menuImg from '@/assets/ivr/menu.svg'
import receiveImg from '@/assets/ivr/receive.svg'
import transferImg from '@/assets/ivr/transfer.svg'
import hangupImg from '@/assets/ivr/hangup.svg'
import satisfactionImg from '@/assets/ivr/satisfaction.svg'
import httpImg from '@/assets/ivr/http.svg'
import jumpImg from '@/assets/ivr/jump.svg'
import wxImg from '@/assets/ivr/wx.svg'
import IfImg from '@/assets/ivr/icon_condition.svg'
import { getIvrKeywordsList, getIvrKeys } from '@/api/call/ivr/index.ts'

export const useIvrStore = defineStore('ivr', {
  state: () => {
    return {
      //画布
      lfRef: null,
      //node list
      nodeList: [
        {
          type: 'playback-node',
          name: '放音',
          des: '用于播放音频文件或文字转语音，通常用作开场的欢迎语和模块间的过渡使用',
          bgc: 'linear-gradient(270deg, #9258f7 0%, #3370FF 100%)',
          img: playBackImg,
          businessType: 2
        },
        {
          type: 'menu-node',
          name: '菜单',
          des: '用于引导流程走向，提供操作选项，实现分支处理',
          bgc: '#229dff',
          img: menuImg,
          businessType: 3
        },
        {
          type: 'receive-node',
          name: '收号',
          des: '用于保存用户输入的按键内容，赋值给自定义参数，传递到分支模块进行处理',
          bgc: '#f759ab',
          img: receiveImg,
          businessType: 4
        },
        {
          type: 'transfer-node',
          name: '转接',
          des: '将客户来电转至相应的技能组、外部电话等',
          bgc: '#14c0ff',
          img: transferImg,
          businessType: 5
        },
        {
          type: 'hangup-node',
          name: '挂机',
          des: '流程结束表示',
          bgc: '#0dbf7d',
          img: hangupImg,
          businessType: 6
        },
        {
          type: 'if-node',
          name: '判断器',
          des: '根据不同条件执行不同节点',
          bgc: '#14C0FF',
          img: IfImg,
          businessType: 11
        },
        // {
        //   type: 'satisfaction-node',
        //   name: '满意度',
        //   des: '用于收集用户满意度，提升用户体验',
        //   bgc: '#9d66e1',
        //   img: satisfactionImg,
        //   businessType: 7
        // },
        {
          type: 'http-node',
          name: '外部调用',
          des: 'http接口调用访问第三方系统，实现客户的自助服务',
          bgc: '#bebf6e',
          img: httpImg,
          businessType: 8
        },
        {
          type: 'jump-node',
          name: '跳转节点',
          des: '可以指定跳转到任意其他流程或者挂机/转人工',
          bgc: '#67c23a',
          img: jumpImg,
          businessType: 9
        },
        {
          type: 'wx-node',
          name: '添加微信',
          des: '指定节点之后添加微信',
          bgc: '#1485ee',
          img: wxImg,
          businessType: 10
        }
      ],
      // 画布节点
      nodes: {
        'start-node': {
          id: 'start',
          type: 'start-node',
          x: 400,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 400,
            height: 180,
            name: '',
            nodeName: '开始',
            businessType: 0
          }
        },
        'end-node': {
          id: 'end',
          type: 'end-node',
          x: 1500,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 400,
            height: 230,
            hangup: false,
            name: '',
            nodeName: '结束',
            businessType: 1
          }
        },
        'playback-node': {
          id: 'playback_',
          type: 'playback-node',
          x: 850,
          y: 400,
          properties: {
            width: 400,
            height: 400,
            hangup: false,
            name: '',
            nodeName: '放音',
            businessType: 2,
            playbackType: 1,
            fileId: '',
            filePath: '',
            content: '',
            num: 1,
            nodeIndex: 0
          }
        },
        'menu-node': {
          id: 'menu_',
          type: 'menu-node',
          x: 1200,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 500,
            height: 853,
            hangup: false,
            inputType: 2, //1 按键  2匹配
            priorityType: 2, //优先设置
            isStartAi: 1,
            name: '',
            nodeName: '菜单',
            businessType: 3,
            interrupt: false,
            playbackType: 1,
            fileId: '',
            filePath: '',
            content: '',
            menuButtons: [
              {
                key: 0,
                buttonName: '',
                buttonValue: '',
                isisEditing: false
              }
            ],
            matching: [],
            notPlaybackType: 1,
            notFileId: '',
            notFilePath: '',
            notContent: '',

            errorPlaybackType: 1,
            errorFileId: '',
            errorFilePath: '',
            errorContent: '',

            timeout: 3000,
            maxRetries: 3,

            aiJumpType: '',
            aiJumpFlowId: '',

            aiNumber: 2,

            asrNotJumpType: '',
            asrNotJumpId: '',
            asrNotFilePath: '',
            asrNotFileType: '',

            asrRepeatJumpType: '',
            asrRepeatJumpId: '',
            asrRepeatFilePath: '',
            asrRepeatFileType: '',

            asrRepeatKeys: ''
          }
        },
        'receive-node': {
          nodeIndex: 0,
          id: 'receive_',
          type: 'receive-node',
          x: 1200,
          y: 400,
          properties: {
            width: 400,
            height: 830,
            name: '',
            nodeName: '收号',
            businessType: 4,
            interrupt: false,
            playbackType: 1,
            fileId: '',
            filePath: '',
            content: '',
            numMax: 0,
            numMin: 0,
            endPoint: '#',
            notPlaybackType: 1,
            notFileId: '',
            notFilePath: '',
            notContent: '',
            errorPlaybackType: 1,
            errorFileId: '',
            errorFilePath: '',
            errorContent: '',
            timeout: 1000,
            maxRetries: 3
          }
        },
        'transfer-node': {
          id: 'transfer_',
          type: 'transfer-node',
          x: 1200,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 400,
            height: 270,
            name: '',
            nodeName: '转接',
            businessType: 5,
            routeType: 4,
            routeValue: ''
          }
        },
        'hangup-node': {
          id: 'hangup_',
          type: 'hangup-node',
          x: 1200,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 400,
            height: 175,
            name: '',
            nodeName: '挂机',
            businessType: 6
          }
        },
        'satisfaction-node': {
          id: 'satisfaction_',
          type: 'satisfaction-node',
          x: 1200,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 400,
            height: 175,
            name: '',
            nodeName: '满意度',
            businessType: 7
          }
        },
        'http-node': {
          id: 'http_',
          type: 'http-node',
          x: 1200,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 500,
            height: 480,
            name: '',
            nodeName: '外部调用',
            businessType: 8,
            method: 1,
            params: '',
            result: '',
            header: [
              {
                key: 0,
                name: '',
                value: '',
                isEditing: false
              }
            ]
          }
        },
        'jump-node': {
          id: 'jump_',
          type: 'jump-node',
          x: 1200,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 500,
            height: 340,
            nodeName: '跳转节点',
            businessType: 9,
            jump_type: '',
            aiword: '',
            name: '',
            label: '',
            ivrFlowId: ''
          }
        },
        'wx-node': {
          id: 'wx_',
          type: 'wx-node',
          x: 1200,
          y: 400,
          properties: {
            nodeIndex: 0,
            width: 400,
            height: 270,
            nodeName: '添加微信',
            name: '',
            businessType: 10,
            userId: [],
            type: 'add-friend',
            add: 1 //是否直接加好友
          }
        },
        'if-node': {
          id: 'if_',
          type: 'if-node',
          x: 1200,
          y: 400,
          properties: {
            name: '',
            nodeIndex: 0,
            width: 500,
            height: 500,
            nodeName: '判断器',
            businessType: 11,
            if_type: '1',
            if_matching: [
              {
                nodeId: '',
                type: '', //1 收号 2 外部调用
                query: '',
                value: ''
              }
            ],
            if_next: '',
            if_else_matching: [
              {
                nodeId: '',
                type: '', //1 收号 2 外部调用
                query: '',
                value: ''
              }
            ],
            if_else_next: '',
            else_next: ''
          }
        }
      },
      nextKey: -1,
      keysList: [],
      refreshOption: false
    }
  },
  getters: {
    getLf() {
      return this.lfRef
    },
    getNodeList() {
      return this.nodeList
    },
    getNodes() {
      return this.nodes
    },
    getNextKey() {
      return this.nextKey
    },
    getKeysList() {
      return this.keysList
    },
    getRefreshOption() {
      return this.refreshOption
    }
  },
  actions: {
    async setLfRef(value: any) {
      this.lfRef = value
    },
    async setNodes(keys: any, value: Object) {
      this.nodes[keys] = value
    },
    async setNextKey() {
      setTimeout(async () => {
        this.keysList = []
        const url = new URL(window.location.href)
        const id = url.searchParams.get('id')

        const data = await getIvrKeywordsList()
        data.push({
          id: '-1',
          branch: '未识别',
          keys: 'XXX'
        })
        const data2 = await getIvrKeys({ flowId: id })
        const arr = [...data, ...data2]
        this.keysList = arr
        this.nextKey = this.nextKey + 1
      }, 1e3)
    },
    async setNextKeyVal(val) {
      this.nextKey = val
    },
    // 获取当前ivr流程分支option
    async setKeysList() {
      this.keysList = []
      const url = new URL(window.location.href)
      const id = url.searchParams.get('id')

      const data = await getIvrKeywordsList()
      data.push({
        id: '-1',
        branch: '未识别',
        keys: 'XXX'
      })
      const data2 = await getIvrKeys({ flowId: id })
      const arr = [...data, ...data2]
      this.keysList = arr
    },
    // 初始节点的index
    async initNodes() {
      for (const key in this.nodes) {
        this.nodes[key].properties.nodeIndex = 0
      }
    },
    // 更新节点的index
    async setNodeIndex(type, val) {
      for (const key in this.nodes) {
        if (this.nodes[key].type == type) {
          if (val) {
            this.nodes[key].properties.nodeIndex = val
          } else {
            this.nodes[key].properties.nodeIndex += 1
          }
        }
      }
    },
    // 回显 节点最大的nodeIndex
    async echoNodeIndex(arr) {
      for (const key in this.nodes) {
        const maxIndex = arr
          .filter((items) => items.type == key)
          .reduce((max, item) => {
            return Math.max(max, item.properties.nodeIndex)
          }, 0)
        this.nodes[key].properties.nodeIndex = maxIndex + 1
      }
    },
    async setRefreshOption(val) {
      this.refreshOption = val
    }
  }
})

export const useIvrStoreWithOut = () => {
  return useIvrStore(store)
}
